﻿Imports System.Data.SqlClient
Public Class Form1
    Private myConn As SqlConnection
    Private myCmd As SqlCommand
    Private myReader As SqlDataReader
    Private strNameDevice As String
    Private Sub Rbackup_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Rbackup.CheckedChanged
        If (Me.Rbackup.Checked = True) Then
            Me.Gbackup.Enabled = True
            Me.Grestore.Enabled = False
            Me.txtNAME.Focus()
        Else
            Me.Gbackup.Enabled = False
            Me.Grestore.Enabled = True
        End If
    End Sub

    Private Sub btnok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnok.Click
        conn.Close()
        Dim cmd_bk As New Odbc.OdbcCommand
        cmd_bk.Connection = conn
        If (Me.Rcompl.Checked = True) Then
            cmd_bk.CommandText = "BACKUP Database QLVT TO QLVT_BACKUP_DEVICE WITH NAME='" + Me.txtNAME.Text + "'"
        ElseIf (Me.Rdiffer.Checked = True) Then
            cmd_bk.CommandText = "BACKUP Database QLVT To QLVT_BACKUP_DEVICE WITH DIFFERENTIAL ,NAME='" + Me.txtNAME.Text + "'"
        End If
        cmd_bk.CommandType = CommandType.Text
        KetnoiCSDL.KetNoi("QLVT")
        Try
            cmd_bk.ExecuteNonQuery()
            Dim i As Integer
            ProgressBar1.Visible = True
            For i = ProgressBar1.Minimum To ProgressBar1.Maximum
                ProgressBar1.Value = i
            Next
            ProgressBar1.Visible = False
        Catch ex As Exception

            MsgBox("Lỗi backup cơ sở dữ liệu QLVT", MsgBoxStyle.OkOnly)
        End Try
        MsgBox("Backup CSDL thành công .")

        conn.Close()
    End Sub

    Private Sub frmbackuprestore_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ProgressBar1.Visible = False
        Dim cmd As Odbc.OdbcCommand = New Odbc.OdbcCommand()
        cmd.Connection = KetnoiCSDL.conn
        'truy cap csdl de lay cac du lieu ve cac thoi diem backup
        cmd = New Odbc.OdbcCommand()
        cmd.Connection = KetnoiCSDL.conn
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "{CALL sp_getThoiDiemBackup}"
        KetnoiCSDL.KetNoi("QLVT")
        'conn.Open()
        Dim rs As Odbc.OdbcDataReader = cmd.ExecuteReader()
        Dim i As Integer = 0
        While (rs.Read())
            Me.cbobkup.Items.Add(rs.GetString(1))
        End While
        rs.Close()
        KetnoiCSDL.conn.Close()
        If (Me.cbobkup.Items.Count > 0) Then
            Me.cbobkup.SelectedIndex = 0
        End If
    End Sub

    Private Sub btnclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclose.Click
        Dim choice As DialogResult
        choice = MessageBox.Show("BẠN CÓ MUỐN THOÁT KHÔNG ?", "XÁC NHẬN THOÁT ?", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If choice = Windows.Forms.DialogResult.Yes Then
            Me.Close()
        End If
    End Sub

    Private Sub cbobkup_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbobkup.SelectedIndexChanged
        txtposition.Text = cbobkup.SelectedValue
    End Sub

    Private Sub btnrest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnrest.Click
        conn.Close() ' Đóng kết nối của chính ta
        Dim conn_temp As New Odbc.OdbcConnection
        Dim str_temp As String
        Dim position As Int16
        Dim ngay
        position = Me.cbobkup.SelectedValue
        ngay = Me.cbobkup.SelectedItem
        str_temp = "DSN=temp;Uid=" & mlogin.Trim & ";Pwd=" & mPass & ";"
        conn_temp.ConnectionString = str_temp
        conn_temp.Open()
        ' Kill các process đang kết tới csdl QLVT
        Dim cmd_kill As Odbc.OdbcCommand
        Dim str As String
        str = "Select spid,loginame from master.dbo.sysprocesses " & _
        " where dbid = ( select dbid from master.dbo.sysdatabases where name = 'QLVT')"
        Dim cmd_process As New Odbc.OdbcCommand
        cmd_process.Connection = conn_temp
        cmd_process.CommandText = str
        cmd_process.CommandType = CommandType.Text
        ' Lấy ds các spID đang kết nối tới QLVT
        Dim myreader As Odbc.OdbcDataReader = cmd_process.ExecuteReader
        If myreader.HasRows Then
            Dim arrID(100) As Integer ' Giả sử tối đa có 101 kết nối
            Dim arrLogin(100) As String
            Dim i As Integer = 0
            Do While myreader.Read
                arrID(i) = myreader.GetInt32(0)
                arrLogin(i) = myreader.GetString(1)
                i = i + 1
            Loop
            Dim m_process As Int32 = i
            Dim j As Int32
            myreader.Close()
            For j = 0 To m_process - 1
                cmd_kill = New Odbc.OdbcCommand
                cmd_kill.CommandType = CommandType.Text
                cmd_kill.Connection = conn_temp
                cmd_kill.CommandText = "Kill " & arrID(j)
                Try
                    cmd_kill.ExecuteNonQuery()
                Catch ex As Exception
                End Try
            Next
        End If
        Try
            Dim cmd_restore As New Odbc.OdbcCommand
            cmd_restore.Connection = conn_temp
            cmd_restore.CommandText = " Restore Database QLVT from QLVT_BACKUP_DEVICE WITH NORECOVERY RESTORE DATABASE QLVT FROM QLVT_BACKUP_DEVICE with FILE = " & CStr(Me.cbobkup.SelectedIndex + 1) & " "
            cmd_restore.CommandType = CommandType.Text
            cmd_restore.ExecuteNonQuery()
            Dim ii As Integer
            ProgressBar1.Visible = True
            For ii = ProgressBar1.Minimum To ProgressBar1.Maximum
                ProgressBar1.Value = ii
            Next
            ProgressBar1.Visible = False
            MsgBox("Restore CSDL thành công.")
            conn_temp.Close()
        Catch ex As Exception
            MsgBox("Lỗi Restore cơ sở dữ liệu QLVT", MsgBoxStyle.OkOnly)
            MsgBox(ex.ToString)
        End Try
    End Sub
    Private Sub txtNAME_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNAME.LostFocus
        txtNAME.Text = KetnoiCSDL.ChuanHoaChuoi(txtNAME.Text.Trim)
        If (Me.txtNAME.Text = "") Then
            MsgBox("Xin hãy nhập tên backup !!!!")
        End If
    End Sub
End Class
